﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Cat.Helper;
using System.IO;

namespace PicServer.Yande
{
    class yande_pool_download
    {
        int page_count = 1;
        /// <summary>
        /// 程序入口类，默认扫描x页
        /// </summary>
        public void List_scanner()
        {
            if (page_count < YandeStaticValue.MaxSacnnerPage)
            {
                new Web_helper().HttpGetAsync(string.Format(YandeStaticValue.yandePool_Link, page_count++), List_complete);
                Console.WriteLine("当前扫描列表页面：{0},链接:{1}",page_count, string.Format(YandeStaticValue.yandePool_Link, page_count));
            }
            else
            {
                Console.WriteLine("列表扫描结束");
                content_Scanner();


            }
        }

        /// <summary>
        /// 扫描图片池列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void List_complete(object sender, OpenReadCompletedEventArgs e)
        {
            string shtml = Html_helper.get_html(e);
            foreach (String line in new Html_helper().Get_href(shtml, "//table[@class='highlightable']/tbody/tr/td[1]/a"))
            {
                if (!Static_value.old_url.Contains(line))
                {
                    YandeStaticValue.yandePool_List.Enqueue("https://yande.re" + line);
                    Console.WriteLine(line);
                }

                else
                    Console.WriteLine("已添加过，不重复添加！");

            }
            content_Scanner();
        }

        /// <summary>
        /// 启动对图片池的扫描
        /// </summary>
        private void content_Scanner()
        {
            Console.WriteLine(  "画集扫描开始");
            if (YandeStaticValue.yandePool_List.Count > 0)
            {
                string link = YandeStaticValue.yandePool_List.Dequeue();
                Console.WriteLine(link);
                YandeStaticValue.old_url = link;
                new Web_helper().HttpGetAsync(link, Page_complete);



            }
            else
            {
                Console.WriteLine("画集列表池处理完毕");
                    List_scanner();
            }
        }
        /// <summary>
        /// 读取图片池并且提取文件名和图片页面的链接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Page_complete(object sender, OpenReadCompletedEventArgs e)
        {
            string shtml = Html_helper.get_html(e);
            string title_rome = new Html_helper().Regex(shtml, "//meta[@property='og:title']")[0].Attributes["content"].Value.Replace("| yande.re", "");
            string title_jp = new Html_helper().Regex(shtml, "//meta[@property='og:description']")[0].Attributes["content"].Value;


            Console.WriteLine("罗马音" + title_rome);
            Console.WriteLine("日语标题" + title_jp);
            //     Console.WriteLine("rome"+title_rome);

            foreach (String line in new Html_helper().Get_href(shtml, "//a[@class='thumb']"))
            {
                Console.WriteLine("temp:" + line);
         

                YandeStaticValue.yandeContent_List.Enqueue(" https://yande.re"+line);

            }
            YandeStaticValue.File_name = new Str_helper().ReplaceBadChar(title_rome + "  " + title_jp);


            //    File.AppendAllText("log.txt", shtml);
            //     content_Scanner();
            Img_get();

        }
        /// <summary>
        /// 扫描并且获取文件链接的类
        /// </summary>
        private void Img_get()
        {
            if (YandeStaticValue.yandeContent_List.Count > 0)
            {
                string link = YandeStaticValue.yandeContent_List.Dequeue();
                new Web_helper().HttpGetAsync(link, img_complete);
            }
            else { Console.WriteLine("所有图片扫描完成");

                Console.WriteLine("图片数量："+YandeStaticValue.yandeFile_List.Count);
                Console.WriteLine(" 文件名："+ YandeStaticValue.File_name);
                Console.WriteLine(" 链接：" + YandeStaticValue.old_url);
                download_start();
            }
        }

    

        private void img_complete(object sender, OpenReadCompletedEventArgs e)
        {
            string shtml = Html_helper.get_html(e);
            foreach (String line in new Html_helper().Get_href(shtml, "//a[@id='highres-show']")) {
                YandeStaticValue.yandeFile_List.Enqueue(line);
                Console.WriteLine(line);
            }
            Img_get();
        }
        private void download_start()
        {
            if (YandeStaticValue.yandeFile_List.Count > 0)
            {
                string url = YandeStaticValue.yandeFile_List.Dequeue();
                string file_path = YandeStaticValue.yandePool_path + YandeStaticValue.File_name + "\\";

                if (new Web_helper().FileDownloader(url, file_path))
                {
                    Console.WriteLine("下载成功");
                  //  new File_helper().Log_Update(Static_value.tag_path + "old_url.txt", url);
                 
                }
                else
                {
                    Console.WriteLine("下载失败");
                }
                download_start();//继续循环下载图片
                return;
            }
            else {

                Console.WriteLine("全部下载完成！");
                new File_helper().Log_Update(Static_value.tag_path + "old_url.txt", YandeStaticValue.old_url.Replace("https://yande.re",""));//添加已下载完成的url
                YandeStaticValue.File_name = "";
                YandeStaticValue.old_url = "";
                content_Scanner();//启动对下一个画集的扫描
            }
         
        }
    }
}
